<template>
  <el-dialog
    v-model="centerDialogVisible"
    :title="paramsTitle"
    align-center
    width="850px"
    draggable
    destroy-on-close
    :close-on-click-modal="false"
  >
    <WsmTable
      ref="table"
      load-url="/dbms-server/base/requestvalidparams/page"
      v-model="queryFormType"
      :change-load="true"
      :is-layout="false"
      table-height="300px"
    >
      <template #title>{{ t('dbms.requestValid.paramsTitle') }}</template>
      <template #query="{ data }">
        <el-form-item :label="t('dbms.requestValid.activity')">
          <el-radio-group v-model="data.activity.value">
            <el-radio value="">{{ t('dbms.requestValid.all') }}</el-radio>
            <el-radio value="0">{{ t('dbms.requestValid.disable') }}</el-radio>
            <el-radio value="1">{{ t('dbms.requestValid.enable') }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </template>
      <template #button="{ row }">
        <el-button type="primary" v-hasPermi="'addParams'" @click="addParamsForm.show()">{{
          t('common.add')
        }}</el-button>
        <el-button v-hasPermi="'updateParams'" v-if="row" @click="update(row.id)" type="success">{{
          t('common.update')
        }}</el-button>
        <el-button v-hasPermi="'deleteParams'" v-if="row" @click="del(row.id)" type="danger">{{
          t('common.delete')
        }}</el-button>
        <el-button
          v-hasPermi="'configValid'"
          v-if="row"
          @click="config(row.id, row.paramName)"
          type="warning"
          >{{ t('dbms.requestValid.configValid') }}</el-button
        >
        <template v-if="row">
          <template v-if="row.activity == 1">
            <el-button v-hasPermi="'checkParams'" @click="check(row.id)" type="danger">{{
              t('dbms.requestValid.disable')
            }}</el-button>
          </template>
          <template v-else>
            <el-button v-hasPermi="'checkParams'" @click="check(row.id)" type="success">{{
              t('dbms.requestValid.enable')
            }}</el-button>
          </template>
        </template>
      </template>
      <template #column>
        <el-table-column prop="activity" :label="t('dbms.requestValid.activity')" width="70">
          <template #default="scope">
            <template v-if="scope.row.activity">
              <el-tag effect="dark" type="success">{{ t('dbms.requestValid.enable') }}</el-tag>
            </template>
            <template v-else>
              <el-tag effect="dark" type="danger">{{ t('dbms.requestValid.disable') }}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="paramKey" :label="t('dbms.requestValid.paramKey')" width="250" />
        <el-table-column prop="paramName" :label="t('dbms.requestValid.paramName')" />
      </template>
    </WsmTable>
    <WsmForm
      ref="addParamsForm"
      v-model="addParamsData"
      submit-url="/dbms-server/base/requestvalidparams/add"
      label-width="100px"
      :init-data="initData"
      :title="t('dbms.requestValid.addParamsTitle')"
      @success="flushed"
    >
      <template #default="{ form }">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="paramKey" :label="t('dbms.requestValid.paramKey')">
              <el-input v-model="form.paramKey" :placeholder="t('dbms.requestValid.paramKey')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="paramName" :label="t('dbms.requestValid.paramName')">
              <el-input v-model="form.paramName" :placeholder="t('dbms.requestValid.paramName')" />
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </WsmForm>

    <WsmForm
      ref="updateParamsForm"
      v-model="updateParamsData"
      load-url="/dbms-server/base/requestvalidparams/get"
      submit-url="/dbms-server/base/requestvalidparams/update"
      label-width="100px"
      :title="t('dbms.requestValid.updateParamsTitle')"
      @success="flushed"
    >
      <template #default="{ form }">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="paramKey" :label="t('dbms.requestValid.paramKey')">
              <el-input v-model="form.paramKey" :placeholder="t('dbms.requestValid.paramKey')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="paramName" :label="t('dbms.requestValid.paramName')">
              <el-input v-model="form.paramName" :placeholder="t('dbms.requestValid.paramName')" />
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </WsmForm>
    <ConfigValid ref="configForm" />
  </el-dialog>
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { useI18n } from '@/hooks/web/useI18n'
import { checkstateApi, delApi } from '@/api/dbms/requestvalidparams'
import { computed, reactive, ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { QueryFormType, QueryType } from '@/components/WsmTable/src/types'
import { FormDataType } from '@/components/WsmForm/src/types'
import ConfigValid from './ConfigValid.vue'

const centerDialogVisible = ref<boolean>(false)

const title = ref()

const paramsTitle = computed(() => {
  return t('dbms.requestValid.paramsTitle') + '[' + title.value + ']'
})

//表格对象
const table = ref()

//新增表单对象
const addParamsForm = ref()
const initData = ref({
  rvid: ''
})
const addParamsData: FormDataType = {
  rvid: '',
  paramKey: '',
  paramName: ''
}

//修改表单对象
const updateParamsForm = ref()
const updateParamsData: FormDataType = {
  id: '',
  rvid: '',
  paramKey: '',
  paramName: ''
}

const { t } = useI18n()

const update = (id: string) => {
  updateParamsForm.value.show({ id })
}

const show = async (id: string, t: string) => {
  initData.value.rvid = id
  queryFormType.rvid.value = id
  title.value = t
  centerDialogVisible.value = true
}
//关闭表单
const hide = () => {
  centerDialogVisible.value = false
}

const queryFormType: QueryFormType = reactive({
  rvid: {
    value: '',
    clear: false
  },
  activity: {
    value: '',
    queryType: QueryType.EQ
  }
})

const del = async (id: string) => {
  ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await delApi({ id })
    if (rs.success) {
      table.value.load(1)
    }
  })
}
const check = async (id: string) => {
  const rs = await checkstateApi({ id })
  if (rs.success) {
    table.value.load()
  }
}

const flushed = () => {
  table.value.load(1)
}

const configForm = ref()
const config = (id: string, title: string) => {
  configForm.value.show(id, title)
}
//暴露方法
defineExpose({ show, hide })
</script>
<style scoped></style>
